// *** LICENSE HEADER ***
// Filename: ./messageviewer.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "messageviewer.h"

messageViewer::messageViewer(int messageNum){
    AllocArrays(messageNum);
}

messageViewer::~messageViewer(){
    DeleteArrays();
}

void messageViewer::SetTimeout(bool doTimeout){
    timeout = doTimeout;
}

void messageViewer::SetMessageNumber(int messageNum){
    //Delete the old arrays and allocate them anew
    DeleteArrays();
    AllocArrays(messageNum);
}

void messageViewer::AddMessage(const char* message){
    //Increase the message index as long as the message at that index is not set
    int i = 0;
    while (i < num && messages[i][0] == 0x00)
        ++i;

    //If all messages are set, shift the messages and thus make place for a new message
    if (i == 0)
        ShiftMessages();
    //Correct the message index, so that it points to the last unused message slot
    else --i;

    //Set the new message data
    strcpy_safe_my(messages[i], message, _MESSAGEVIEWER_BUFSIZE);
    times[i] = _MESSAGEVIEWER_TIMEOUT;
}

void messageViewer::SetTop(int newTop){
    top = newTop;
}

void messageViewer::Update(int dt){
    //Return if no messages are set or if we don't have to delete old messages
    if (messages[num-1][0] == 0x00 || !timeout) return;

    //Decrease remaining life times of all messages
    for (int i = 0; i < num; ++i)
        times[i] -= dt;
    //If there's a message that is old, remove it and shift all other messages
    if (num >= 1 && times[num-1] <= 0)
        ShiftMessages();
}

void messageViewer::Draw(fonts* fontDrawer){
    SDL_Color white; white.r = white.g = white.b = 255;
    for (int i = 0; i < num; ++i){
        fontDrawer->Print(FT_MENU, messages[i], 10, top-(num-i-1)*20, _MESSAGEVIEWER_FONTSIZE, white);
    }
}

void messageViewer::DeleteArrays(){
    for (int i = 0; i < num; ++i){
        delete[] messages[i];
    }
    delete[] messages;
    delete[] times;
}

void messageViewer::AllocArrays(int messageNum){
    messages = new char*[messageNum];
    for (int i = 0; i < messageNum; ++i){
        messages[i] = new char[_MESSAGEVIEWER_BUFSIZE];
        messages[i][0] = 0x00;
    }
    times = new int[messageNum];

    num = messageNum;
}

void messageViewer::ShiftMessages(){
    //Just shift the array members except for the last one and set the first one to zero
    char* swapCp = messages[num-1];
    int swapI = times[num-1];
    for (int i = num-1; i > 0; --i){
        times[i] = times[i-1];
        messages[i] = messages[i-1];
    }
    times[0] = 0;
    messages[0] = swapCp;
    messages[0][0] = 0x00;
}
